let mor = require("../lib/mongo/mor");
let jv = require("../lib/sys/myHelper")

let userService = {
    async getUserInfo(user_id) {
        if (!user_id) return {};
        return await mor.sys.user.queryById(user_id).toEntity();
    },

    async setUserInfo(user) {

    },
    async getRoomList(user_id) {
        if (!user_id) return [];

        return await mor.im.room.query()
            .where({"users.id": user_id})
            .select({name: 1})
            .order({createAt: -1})
            .toList();
    },
    async getFriends(user_id) {
        if (!user_id) return [];

        let ret = await mor.im.friend.query()
            .where({$or: [{"user.id": user_id}, {"friend.id": user_id}]})
            .order({createAt: -1})
            .toList();

        return ret
            .map(it => {
                return {id: it.friend.id, name: it.friend.name};
            });
    },

    async getMine(user_id){

    }
};


Object.assign(exports, userService);